package gui;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import client.ComplaintDetails;
import client.Main;
import client.MemberAccount;



public class Complaint_Gui extends JFrame implements ActionListener, TableModelListener {

	/**
	 * complaint types
	 */
	public String[] choice={"non-handled","handled"};
    /**
     * headers for complaint table
     */
	public String[] columnNames = {"Complaint ID", "User ID", "Complaint Description","Response","compensation"};
    /**
     * stores the details that come from database
     */
	public Object[][] data = null;
	/**
	 * store details to send to echo server
	 */
    public String sn[]=new String[2];
    /**
     * complaint response
     */
    public String resp=null; 
    /**
     * complaint id
     */
    public String cid=null;
   
    
    //Table
	JTable table;
	
    //panels
	JPanel panel;
	JPanel panelTable;
	
	//buttons
	public JButton btnBack;
	public JButton btnBack1;
	public JButton btnSubmit;
	public JButton btnLogOut;
	
	//labels
	JLabel lblHeader;
	JLabel lblHeaderTable;
	JLabel lblText;
	
	//scroll option
	JScrollPane scroll;
	
	//combo box
	JComboBox options;
	
	//text
	JTextField txt;
	
	Container contentPane;
	
	
	JComboBox getCombo()
	{
		if(options==null)
		{
			options=new JComboBox();
			for(int i=0;i<2;i++)
				options.addItem(choice[i]);
				options.setBounds(180, 200, 140, 40);
		}
		return options;
	}
	/**
	 * build frame and main panel
	 */
	public Complaint_Gui ()
	{
			super();
			contentPane = this.getContentPane();
		    contentPane.setLayout(new BorderLayout(0, 0));
			this.setBounds((int)Main.dim.getWidth()/2-260, (int)Main.dim.getHeight()/2-275, 515, 550);
			this.setResizable(false);
			this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			contentPane.add(getPanel(),BorderLayout.CENTER);
			this.setSize(500,400);
			this.setVisible(true);
	}
	
	JPanel getPanel() 
	{	
		if (panel == null){
			panel = new JPanel();
			panel.setSize(500, 400);
			panel.setLayout(null);
			panel.add(getBtnBack());
			panel.add(getBtnSubmit());
			panel.add(getLabelHeader());
			panel.add(getCombo());
			panel.add(getLblText());
			panel.add(getBtnLogOut());
		}
		return panel;
	}
	
	JPanel getPanelTable()
		{
		if (panelTable == null)
			{
			panelTable = new JPanel();
			panelTable.setSize(1000, 400);
			panelTable.setLayout(null);
			panelTable.add(getTable());
			panelTable.add(getBtnBack1());
			panelTable.add(getLabelHeaderTable());
			panelTable.add(getBtnLogOut());
		//	panelTable.add(getBtn());
			}
			return panelTable;
		}
		
	JButton getBtnBack(){
		
		if (btnBack == null){
			btnBack = new JButton();
			btnBack.setBounds(380, 20, 100, 40);
			btnBack.setText("Back");
		}
		return btnBack;
	}
	
	JButton getBtnBack1(){
		
		if (btnBack1 == null){
			btnBack1 = new JButton();
			btnBack1.setBounds(875, 20, 100, 40);
			btnBack1.setText("Back");
			btnBack1.addActionListener(this);
		}
		return btnBack1;
	}
	
	JButton getBtnLogOut() {
		
		if (btnLogOut == null){
		btnLogOut = new JButton();
		btnLogOut.setBounds(20,20,100,40);
		btnLogOut.addActionListener(this);
		btnLogOut.setText("LogOut");
		}
		return btnLogOut;
	}
	
	JButton getBtnSubmit(){
		
		if (btnSubmit == null){
			btnSubmit = new JButton();
			btnSubmit.setBounds(340, 200, 150, 40);
			btnSubmit.setText("Submit");
			btnSubmit.addActionListener(this);
			
		}
		return btnSubmit;
	}
		
	JLabel getLblText()
	{
		if(lblText==null)
		{
			lblText=new JLabel();
			lblText.setText("Complaint types");
			lblText.setBounds(20, 200, 140, 40);
			lblText.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblText;
	}
	
	JLabel getLabelHeader()
	{
		if(lblHeader==null)
		{
			lblHeader=new JLabel();
			lblHeader.setText("Complaint Handeling");
			lblHeader.setBounds(130, 70, 260, 40);
			lblHeader.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeader;
	}
	
	JLabel getLabelHeaderTable()
	{
		if(lblHeaderTable==null)
		{
			lblHeaderTable=new JLabel();
			lblHeaderTable.setText("Complaints List");
			lblHeaderTable.setBounds(350, 70, 260, 40);
			lblHeaderTable.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeaderTable;
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == btnSubmit)
		{
			panelTable=null;
			sn[0] = "get complaints";
			sn[1] = (String) options.getSelectedItem();
			data = ComplaintDetails.getComplaintDetails(sn);
			if(data.length==0)
				JOptionPane.showMessageDialog(null, "There are no complaints!");
			else
			{
			this.getContentPane().removeAll();
			this.setBounds(150, 260, 1000, 400);
			this.getContentPane().add(getPanelTable(),BorderLayout.CENTER);
			validate();
			setVisible(true);
			}
		}
		
		if(e.getSource() == btnLogOut)
		{
			if(JOptionPane.showConfirmDialog(null,"Are you sure you want to logout?",null,JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
			{
				this.setEnabled(false);
				this.setVisible(false);
				Main.main.logOut.doClick();
			}
		}
		
		if(e.getSource() == btnBack1)
		{
			panel=null;
			this.getContentPane().removeAll();
			this.setBounds((int)Main.dim.getWidth()/2-260, (int)Main.dim.getHeight()/2-275, 505, 390);
			this.getContentPane().add(getPanel(),BorderLayout.CENTER);
			this.setSize(500, 400);
			validate();
			setVisible(true);
		}	
	}
	
	
	
	
	JScrollPane getTable(){
		
		  DefaultTableModel model = new DefaultTableModel(data, columnNames){
			  public boolean isCellEditable(int rowIndex, int mColIndex) {
			      if(mColIndex>2)
			    	  return true;
			      else return false;
			      }
		  };
		  JTable table = new JTable(model); 
		  table.getModel().addTableModelListener(this);
		  TableColumn column = table.getColumnModel().getColumn(2);
		  column.setPreferredWidth(200);
		  column = table.getColumnModel().getColumn(0);
		  column.setPreferredWidth(1);
		  column = table.getColumnModel().getColumn(1);
		  column.setPreferredWidth(1);
	      JScrollPane scroll = new JScrollPane(table); 
	      scroll.setBounds(90, 120, 800, 190);
	      table.setBounds(90, 120, 800, 190);
	      return scroll; 
	}
	
	
	/**
	 * insert or delete complaint responses and adds compensation
	 */
	public void tableChanged(TableModelEvent e) {
		
		TableModel model = (TableModel)e.getSource();
		int row = e.getFirstRow();
		int column=e.getColumn();
		resp = (String) model.getValueAt(row, column);
		
		
		if(column==3)
		{
			cid = (String) model.getValueAt(row, 0);
			ComplaintDetails.updateData(cid,resp);
			if(resp.isEmpty())
				JOptionPane.showMessageDialog(null, "Response has been deleted successfully!");
			else
				JOptionPane.showMessageDialog(null, "Response has been added successfully!");
		}
		
		if(column==4)
		{
			String compID = (String) model.getValueAt(row, 0);
			cid = (String) model.getValueAt(row, 1);
			if(resp.isEmpty())
				JOptionPane.showMessageDialog(null, "You must insert an amount!");
			else
			{
				MemberAccount.SetBalance(cid,resp,compID);
				JOptionPane.showMessageDialog(null, "Compensation has been added successfully!");
			}
		}	
    }

}
	

